package com.android.util;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.android.R;


/**
 * author TenFace
 */
public class SimplePrompt {

    private static SimplePromptDialog dialog;
    private Context context;
    private static SimplePrompt simple = null;

    public static SimplePrompt getIntance() {
        if (simple == null) {
            simple = new SimplePrompt();
        }
        return simple;
    }

    public void showLoadingMessage(Context context, String msg, boolean cancelable) {
        try {
            dismiss();
            setDialog(context, msg, R.mipmap.simplehud_spinner, cancelable);
            if (dialog != null)
                dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLoadingMessage(Context context, String msg, boolean cancelable, int sleepTime) {
        try {
            dismiss();
            setDialog(context, msg, R.mipmap.simplehud_spinner, cancelable);
            if (dialog != null)
                dialog.show();
            dismissAfter2s(sleepTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 等待框是否正在显示
     *
     * @return
     */
    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    public void showErrorMessage(Context context, String msg) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_error, true);
        if (dialog != null) {
            dialog.show();
            //dismissAfter2s();
            dismissAfter2s(2);
        }
    }

    public void showErrorMessage(Context context, String msg, int sleepTime) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_error, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s(sleepTime);
        }
    }

    public void showSuccessMessage(Context context, String msg) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_success, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s();
        }
    }

    public void showSuccessMessage(Context context, String msg, int sleepTime) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_success, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s(sleepTime);
        }
    }

    public void showInfoMessage(Context context, String msg) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_info, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s();
        }
    }

    public void showInfoMessage(Context context, String msg, int sleepTime) {
        dismiss();
        setDialog(context, msg, R.mipmap.simplehud_info, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s(sleepTime);
        }
    }

    private void setDialog(Context ctx, String msg, int resId, boolean cancelable) {
        try {
            if (ctx == null) {
                return;
            }
            context = ctx.getApplicationContext();

            if (!isContextValid())
                return;

            dialog = SimplePromptDialog.createDialog(ctx);
            dialog.setMessage(msg);
            dialog.setImage(ctx, resId);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(cancelable); // back键是否可dimiss对话框
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void dismiss() {
        try {
            if (isContextValid() && dialog != null && dialog.isShowing())
                dialog.dismiss();
            dialog = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 计时关闭对话框
     */
    private void dismissAfter2s() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1200);
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 计时关闭对话框
     */
    private void dismissAfter2s(final int sleepTime) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(sleepTime * 1000);
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0)
                dismiss();
        }
    };

    /**
     * 判断parent view是否还存在 若不存在不能调用dismiss，或setDialog等方法
     *
     * @return
     */
    private boolean isContextValid() {
        try {
            if (context == null)
                return false;
            if (context instanceof Activity) {
                Activity act = (Activity) context;
                if (act.isFinishing())
                    return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
